package com.hulk.ratel.persistence.handler;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.hulk.ratel.common.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import java.time.LocalDateTime;

/**
 * @author hulk
 * @date 2019/10/29
 */
@Slf4j
public class CommonMetaObjectHandler implements MetaObjectHandler {

	/**
	 * 创建时间
	 */
	private static final String CREATED_TIME = "createdTime";
	/**
	 * 修改时间
	 */
	private static final String UPDATED_TIME = "updatedTime";
	/**
	 * 创建者ID
	 */
	private static final String CREATED_BY = "createdBy";
	/**
	 * 修改者ID
	 */
	private static final String UPDATED_BY = "updatedBy";
	/**
	 * 乐观锁字段
	 */
	public static final String REVERSION = "reversion";



	@Override
	public void insertFill(MetaObject metaObject) {
		log.debug("start insert fill ....");
		this.strictInsertFill(metaObject, CREATED_TIME, LocalDateTime.class, LocalDateTime.now());
		this.strictInsertFill(metaObject, CREATED_BY, String.class, Constants.DEFUAULT_CREATER);
		this.strictInsertFill(metaObject, REVERSION, Long.class, 0L);
	}

	@Override
	public void updateFill(MetaObject metaObject) {
		log.debug("start update fill ....");
		this.strictUpdateFill(metaObject, UPDATED_TIME, LocalDateTime.class, LocalDateTime.now());
		this.strictUpdateFill(metaObject, UPDATED_BY, String.class, Constants.DEFUAULT_UPDATE);

	}


}
